use hysteria_server::{
    auth::{HttpAuthenticator, PasswordAuthenticator, UserPassAuthenticator},
    HysteriaServer, ServerConfig,
};
use std::sync::Arc;
use tracing::info;

pub async fn run(config_path: &str) -> anyhow::Result<()> {
    info!("Loading server configuration from: {}", config_path);

    let config = ServerConfig::from_file(config_path)?;

    // Create authenticator based on config
    let authenticator: Arc<dyn hysteria_server::Authenticator> = match &config.auth {
        hysteria_server::config::AuthConfig::Password { password } => {
            Arc::new(PasswordAuthenticator::new(password.clone()))
        }
        hysteria_server::config::AuthConfig::UserPass { userpass } => {
            Arc::new(UserPassAuthenticator::new(userpass.clone()))
        }
        hysteria_server::config::AuthConfig::Http { url, insecure } => {
            Arc::new(HttpAuthenticator::new(url.clone(), *insecure)?)
        }
    };

    let server = HysteriaServer::new(config, authenticator)?;

    info!("Starting Hysteria server...");
    server.serve().await?;

    Ok(())
}
